package tempo.timing;

import java.sql.Time;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class FinishActivity extends Activity
{

	private static class EfficientAdapter extends BaseAdapter
	{
		private LayoutInflater mInflater;
		private ArrayList<Rider> mRiders;

		public EfficientAdapter(Context context, ArrayList<Rider> riders)
		{
			mInflater = LayoutInflater.from(context);
			mRiders = riders;
		}

		public int getCount()
		{
			return mRiders.size();
		}

		public Object getItem(int position)
		{
			return position;
		}

		public long getItemId(int position)
		{
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			ViewHolder holder;
			if (convertView == null)
			{
				convertView = mInflater.inflate(R.layout.listview, null);
				holder = new ViewHolder();
				holder.text = (TextView) convertView.findViewById(R.id.TextView01);
				holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);
				convertView.setTag(holder);
				convertView.setBackgroundColor((position & 1) == 1 ? Color.WHITE : Color.LTGRAY);
				if(mRiders.get(position).getFinishTime()> 0)
					convertView.setBackgroundColor(Color.GREEN);

			} else
			{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.text.setText(mRiders.get(position).getStartNumber());
			holder.text2.setText(mRiders.get(position).getRiderName());
			return convertView;
		}

		static class ViewHolder
		{
			TextView text;
			TextView text2;
		}
	}

	private Button btnSet;
	private Button btnFinish;
	private ListView listViewRiders;
	private ListView listViewTimes;
	private ArrayList<Rider> mRiders;
	private ArrayList<Long> mTimes;
	private TextView txtStartNumber;
	private TextView txtRiderName;
	private TextView txtFinishTime;
	private int selectedRider = -1;
	private int selectedTime = -1;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.finish);

		txtStartNumber = (TextView) findViewById(R.id.txtStartNumberFinish);
		txtRiderName = (TextView) findViewById(R.id.txtRiderNameFinish);
		txtFinishTime = (TextView) findViewById(R.id.txtFinishTime);

		txtStartNumber.setText("");
		txtRiderName.setText("");
		txtFinishTime.setText("");

		btnSet = (Button) findViewById(R.id.btnSet);
		btnSet.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				if (selectedRider > -1 && selectedRider < mRiders.size() && selectedTime > -1 && selectedTime < mTimes.size())
				{
					Rider rider = mRiders.get(selectedRider);
					rider.setFinishTime(mTimes.get(selectedTime));
					String message = rider.getRiderName() + " has finished in " + rider.GetStringResult();
					ToastHelper.DisplayToast(getBaseContext(), message);
					Race.getCurrent().SaveJson();
					//clear rider info from screen
					setFinishTime(-1);
					setRider(-1);
					refreshList();
				}
			}
		});

		btnFinish = (Button) findViewById(R.id.btnFinish);
		btnFinish.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				Race.getCurrent().AddFinishTime(System.currentTimeMillis());
				setTimesAdapter();
			}
		});

		listViewTimes = (ListView) findViewById(R.id.lstTimes);
		listViewTimes.setDividerHeight(2);
		listViewTimes.setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
			{
				setFinishTime(arg2);
			}
		});

		listViewRiders = (ListView) findViewById(R.id.lstRiders);
		ColorDrawable divcolor = new ColorDrawable(Color.DKGRAY);
		listViewRiders.setDivider(divcolor);
		listViewRiders.setDividerHeight(2);
		listViewRiders.setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
			{
				setRider(arg2);
			}
		});
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		refreshList();

		setTimesAdapter();
	}
	
	@SuppressWarnings("unchecked")
	private void refreshList()
	{
		mRiders = (ArrayList<Rider>) Race.getCurrent().GetRiders().clone();
		listViewRiders.setAdapter(new EfficientAdapter(this, mRiders));
	}

	private void setTimesAdapter()
	{
		mTimes = Race.getCurrent().GetFinishTimes();
		ArrayAdapter<Long> adapter = new ArrayAdapter<Long>(this, R.layout.simplelistview, mTimes)
		{
			public View getView(int position, View convertView, ViewGroup parent)
			{
				View row;

				if (null == convertView)
				{
					row = LayoutInflater.from(this.getContext()).inflate(R.layout.simplelistview, null);
				} else
				{
					row = convertView;
				}

				TextView tv = (TextView) row.findViewById(R.id.txtTimes);
				tv.setText(new Time(getItem(position)).toString());

				return row;
			}
		};
		listViewTimes.setAdapter(adapter);
	}

	private void setFinishTime(int index)
	{
		if (index == -1)// clear
		{
			selectedTime = index;
			txtFinishTime.setText("");
		} else
		{
			selectedTime = index;
			txtFinishTime.setText(new Time(mTimes.get(selectedTime)).toString());
		}
	}

	private void setRider(int index)
	{
		if (index == -1)
		{
			selectedRider = index;
			txtStartNumber.setText("");
			txtRiderName.setText("");
		} else
		{
			selectedRider = index;
			txtStartNumber.setText(mRiders.get(selectedRider).getStartNumber());
			txtRiderName.setText(mRiders.get(selectedRider).getRiderName());
		}
	}
}
